package cn.t.service.order;

import cn.t.constants.CommonConstants;
import cn.t.constants.ParamConstants;
import cn.t.constants.ResponseError;
import cn.t.core.exception.BizException;
import cn.t.core.mode.PageQueryDTO;
import cn.t.core.service.ServiceImpl;
import cn.t.dto.order.OrderQueryRequest;
import cn.t.dto.order.QuotationQueryRequest;
import cn.t.enums.order.MsvmOrderStatusEnum;
import cn.t.enums.order.OrderTransferStatusEnum;
import cn.t.enums.order.OrderTypeEnum;
import cn.t.model.order.OrderDO;
import cn.t.repository.order.OrderRepository;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 领料订单表(Order)领域服务
 *
 * @author t
 * @since 2024-05-24 10:19:12
 */
@Slf4j
@Service
@AllArgsConstructor
public class OrderService extends ServiceImpl<OrderDO, OrderRepository> {
    public OrderDO findInfoByModel(OrderDO dataDo) {
        return repository.findInfoByModel(dataDo);
    }

    public Boolean removeOrderById(Long orderId) {
        return repository.removeOrderById(orderId);

    }

    public OrderDO getOrderDO(String orderSn) {
        if (ObjectUtils.isEmpty(orderSn)) {
            throw new BizException(ResponseError.PARAM_NOT_FOUND, String.format(ResponseError.PARAM_NOT_FOUND.getName(), "orderSn"));
        }
        OrderDO orderDO = new OrderDO();
        orderDO.setOrderSn(orderSn);
        OrderDO infoByModel = findInfoByModel(orderDO);
        if (ObjectUtils.isEmpty(infoByModel)) {
            throw new BizException(ResponseError.ORDER_NOT_EXIST, String.format(ResponseError.ORDER_NOT_EXIST.getName(), orderSn));
        }
        return infoByModel;
    }

    public List<OrderDO> getList(PageQueryDTO<OrderQueryRequest> req, OrderTypeEnum orderType) {
        getParam(req.getParam(), orderType);
        return repository.queryOrderList(req.getParam(), req.getStart(), req.getPageSize(), ParamConstants.FIELD, ParamConstants.DESC);

    }


    public List<OrderDO> getList(OrderQueryRequest request, OrderTypeEnum orderType) {
        getParam(request, orderType);
        return repository.queryOrderList(request, CommonConstants.ZERO, Integer.MAX_VALUE, null, null);
    }

    private static void getParam(OrderQueryRequest param, OrderTypeEnum orderType) {

        if (OrderTypeEnum.ORDER.equals(orderType)) {
            param.setOrderStatusEnum(MsvmOrderStatusEnum.NORMALCODES);
        } else if (OrderTypeEnum.TRANSFER_ORDER.equals(orderType)) {
            ArrayList<Integer> objects = new ArrayList<>();
            objects.add(MsvmOrderStatusEnum.CHANGETONORMAL.getCode());
            param.setOrderStatusEnum(objects);
            param.setOrderTransferStatus(OrderTransferStatusEnum.TRANSFERRED.getCode());
        }
    }

    public List<OrderDO> getErrList(PageQueryDTO<OrderQueryRequest> req) {
        OrderQueryRequest param = req.getParam();
        return repository.queryErrOrderList(param, req.getStart(), req.getPageSize(), ParamConstants.FIELD, ParamConstants.DESC);
    }

    public Long queryErrOrderCount(OrderQueryRequest orderQueryDO) {
        return repository.queryErrOrderCount(orderQueryDO);
    }

    public Long queryOrderCount(OrderQueryRequest orderQueryDO, OrderTypeEnum orderType) {
        getParam(orderQueryDO, orderType);
        return repository.queryOrderCount(orderQueryDO, orderType);
    }

    public List<OrderDO> orderQuoteOrders(QuotationQueryRequest orderQueryDO) {
        return repository.orderQuoteOrders(orderQueryDO);
    }

    public Boolean checkOrder(List<String> orderSnList) {
        QuotationQueryRequest quotationQueryRequest = new QuotationQueryRequest();
        quotationQueryRequest.setOrderSnList(orderSnList);
        List<OrderDO> orders = orderQuoteOrders(quotationQueryRequest);
        return orders.stream().map(OrderDO::getFirmId).distinct().count() == 1;
    }

    public void updateSyncStatus(List<String> orderSnList, Integer syncStatus) {
        OrderDO orderDO = new OrderDO();
        orderDO.setSyncStatus(syncStatus);
        orderDO.setOrderSns(orderSnList);
        repository.updateOrder(orderDO);
    }
}
